cmake_minimum_required(VERSION 3.23)
project(qt6_test)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
#set(Leptonica_DIR "D:/software/msys64/mingw64/lib/cmake/Leptonica" CACHE PATH "Leptonica CMake directory")

set(CMAKE_PREFIX_PATH "D:/software/msys64/mingw64/lib/cmake")

set(AUTOITX_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_part/" )

find_file(AUTOITX_DLL "AutoItX3_x64.dll" PATHS ${AUTOITX_DIR})
if(NOT AUTOITX_DLL)
    message(FATAL_ERROR "未找到 AutoItX3_x64.dll 文件，请检查 AUTOITX_DIR 路径设置。")
endif()

# 查找 AutoItX3_x64.lib 文件（如果有的话）
find_library(AUTOITX_LIB "AutoItX3_x64_DLL.lib" PATHS ${AUTOITX_DIR})

find_package(Qt6 COMPONENTS
        Core
        Gui
        Widgets
        REQUIRED)
#if(NOT TARGET Leptonica::Leptonica)
#    # 创建导入的共享库目标
#    add_library(Leptonica::Leptonica SHARED IMPORTED)
#
#    # 设置目标的属性：位置
#    set_target_properties(Leptonica::Leptonica PROPERTIES
#            # 导入库的接口包含目录
#            INTERFACE_INCLUDE_DIRECTORIES "D:/software/msys64/mingw64/include/leptonica"
#            # 导入库在 Debug 配置下的共享库位置
#            IMPORTED_LOCATION_DEBUG "D:/software/msys64/mingw64/bin/liblept.dll"
#            # 导入库在 Release 配置下的共享库位置
#            IMPORTED_LOCATION_RELEASE "D:/software/msys64/mingw64/bin/liblept.dll"
#            # 导入库在 Debug 配置下的导入库位置（Windows 下）
#            IMPORTED_IMPLIB_DEBUG "D:/software/msys64/mingw64/lib/liblept.dll.a"
#            # 导入库在 Release 配置下的导入库位置（Windows 下）
#            IMPORTED_IMPLIB_RELEASE "D:/software/msys64/mingw64/lib/liblept.dll.a"
#    )
#
#    # 检查是否有依赖项
#    if("")
#        # 列出依赖项
#        set_property(TARGET Leptonica::Leptonica APPEND PROPERTY
#                INTERFACE_LINK_LIBRARIES "")
#    endif()
#endif()

#set(Leptonica_LIBRARY D:/software/msys64/mingw64/lib/liblept.dll.a)
#set(Leptonica_DYNAMIC_LIBRARY D:/software/msys64/mingw64/bin/liblept.dll)


# 查找 OpenCV 库
find_package(OpenCV REQUIRED)
#find_package(SW REQUIRED)
#寻找 LEPTONICA 库
FIND_PACKAGE(Leptonica  REQUIRED)
#寻找 TESSERACT 库
find_package(Tesseract REQUIRED)
#link_directories(D:/software/msys64/mingw64/lib)
#link_directories(D:/software/msys64/mingw64/bin)
# 检查是否为导入的目标
# 检查是否为导入的目标

# 包含 OpenCV 头文件目录
include_directories(${OpenCV_INCLUDE_DIRS})
#  Tesseract 库
include_directories(${Tesseract_INCLUDE_DIRS})
# Leptonica 库
include_directories(${Leptonica_INCLUDE_DIRS})
include_directories(./includes)

add_library(taskLib SHARED task/TaskWindow.cpp task/TaskWindow.h task/GlobalContext.cpp task/GlobalContext.h task/WindowControl.cpp task/WindowControl.h
        task/SSUtil.cpp
        task/SSUtil.h
        task/ExecuteThread.cpp task/ExecuteThread.h
        task/TaskType.h
        task/TaskStep.h
        task/SSPoint.h
        task/ActionKey.cpp
        task/ActionKey.h
        task/TaskStatus.cpp
        task/TaskStatus.h
        task/taskstep/MallTaskStep.h
        task/taskdetail/AutoBattle.cpp
        task/taskdetail/AutoBattle.h task/taskstep/CheckMasterGateTaskFinish.h)
target_link_libraries(taskLib PRIVATE Qt6::Core Qt6::Widgets Qt::Gui ${OpenCV_LIBS} ${AUTOITX_LIB})

add_library(uiLib SHARED ui/MainWindow.cpp ui/MainWindow.h)
target_link_libraries(uiLib PRIVATE Qt6::Core Qt6::Widgets Qt::Gui ${OpenCV_LIBS} taskLib ${AUTOITX_LIB}
        ${Tesseract_LIBRARIES}
        ${Leptonica_LIBRARIES}
#        ${Leptonica_DYNAMIC_LIBRARY}
)

target_include_directories(uiLib PRIVATE  task)
include_directories(./ui ./task ./third_part)

add_executable(qt6_test main.cpp )
target_link_libraries(qt6_test
        PRIVATE
        Qt::Core
        Qt::Gui
        Qt::Widgets
        uiLib
        taskLib
#        ${Leptonica_DYNAMIC_LIBRARY}
#        ${Tesseract_LIBRARIES}
#        ${Leptonica_LIBRARIES}
        )
target_include_directories(qt6_test PRIVATE ui task)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/resources DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/third_part DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
if (WIN32)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()


# ========== 新增Google Test配置 ==========

# 添加Google Test
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# 添加测试可执行文件
add_executable(
        lib_tests
        tests/taskLib_tests.cpp)

# 链接Google Test和被测库
target_link_libraries(
        lib_tests
        GTest::gtest_main
        taskLib
        uiLib
        Qt6::Core
        Qt6::Widgets
        Qt::Gui
        ${OpenCV_LIBS}
        ${AUTOITX_LIB}
        ${Tesseract_LIBRARIES}
        ${Leptonica_LIBRARIES}
        ${GTEST_LIBRARIES}
        ${GTEST_MAIN_LIBRARIES}
        pthread  # 必须链接pthread
)